package com.xiaostudy.controller.userModel;

//用户登陆

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class UserLoginController {

    @RequestMapping("user/toIndex")
    public String toIndex(){
        return "index";
    }

    @RequestMapping("/user/login")
    public String login(){
        return "admin/login";
    }

    //链接到文章管理
    @RequestMapping("/user/toadminIndex")
    public String toadminIndex(String usercount, String password, Model model){
        Subject subject = SecurityUtils.getSubject();   //获取当前用户
        UsernamePasswordToken token = new UsernamePasswordToken(usercount, password);   //shiro要用

        try {
            subject.login(token);       //登陆
            return "admin/blogs";       //成功进入到后台修改页面
        }catch (UnknownAccountException exception){
            model.addAttribute("msg","账号");
            return "admin/login";   //账号错误返回到登陆页面
        }catch (IncorrectCredentialsException exception){
            model.addAttribute("msg","密码错误");
            return "admin/login";       //密码错误也返回到登陆页面
        }

    }

    //链接到分类管理
    @RequestMapping("/user/toTypes")
    public String toType(){
        return "admin/types";
    }

    //链接到标签管理
    @RequestMapping("/user/toTags")
    public String toTags(){
        return "admin/tags";
    }

    //链接到编辑页面
    @RequestMapping("/user/edit")
    public String toedit(){
        return "admin/blogs-input";
    }


    @RequestMapping("/noauth")
    @ResponseBody
    public String noAuthoritory(){
        return "非法访问";
    }

}
